package com.ruoyi.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AccountingSync implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(AccountingSync.class);
    /*共享资源*/
    static int i = 0;

    @Override
    public  void run() {
        for (int i = 0; i < 1000000; i++) {
            increase();
        }
    }

    private synchronized static void increase() {
        i++;
    }

    public static void main(String[] args) throws InterruptedException {
        try {
            AccountingSync sync1 = new AccountingSync();
            AccountingSync sync2 = new AccountingSync();
            logger.debug(sync1.toString());
            logger.debug(sync2.toString());
            Thread t1 = new Thread(sync1);
            Thread t2 = new Thread(sync2);
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            logger.debug(String.valueOf(i));
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }

}
